/*
 * 	Flan.Zeng 2011-2016	http://git.oschina.net/signup?inviter=flan
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.flan.stock.fragment;

import java.util.List;

import com.flan.stock.R;
import com.flan.stock.activity.HSAStockActivity;
import com.flan.stock.activity.QuotationActivity;
import com.flan.stock.activity.StockDetailActivity;
import com.flan.stock.adapter.StockRankListViewAdapter;
import com.flan.stock.bean.Stock;
import com.flan.stock.service.StockService;
import com.flan.stock.service.UIListener;
import com.flan.stock.utils.Constant;
import com.flan.stock.view.NoScrollListView;
import com.flan.stock.view.ObservableScrollView;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

/**
 * 市场行情界面中，显示沪深涨跌排行榜的Fragment
 * @author flan
 * @date   2015年10月28日
 */
public class FragmentQuotationHs extends Fragment implements UIListener{
	
	//private ScrollView scrollView;
	private ObservableScrollView scrollView;
	private NoScrollListView nslv_zfb;
	private TextView tv_zfb;
	private RelativeLayout zfbTitle;
	private QuotationActivity parentActivity;
	private Context context;
	//private ArrayList<Stock> stockList;
	//public static List<Stock> stockList;
	private StockRankListViewAdapter listViewAdapter;
	
	private boolean zfbIsClose = false;
	
	public FragmentQuotationHs(Context context) {
		super();
		this.context = context;
	}
	
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		parentActivity = (QuotationActivity) getActivity();
		
		StockService.getQuotationRank(0,10,Constant.STOCK_COLUMN_NOWPRICE,Constant.ORDER_BY_DESC,1002,this);
		
	}

	@SuppressLint("InflateParams")
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		
		
		scrollView = (ObservableScrollView) inflater.inflate(R.layout.fragment_quotation_hs, null);
		
		zfbTitle = (RelativeLayout) scrollView.findViewById(R.id.rl_hs_zfb);
		
		tv_zfb = (TextView) scrollView.findViewById((int) R.id.tv_zfb);
		
		nslv_zfb = (NoScrollListView) scrollView.findViewById(R.id.lv_hs_zfb);
		listViewAdapter = new StockRankListViewAdapter(context);
		nslv_zfb.setAdapter(listViewAdapter);
		nslv_zfb.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				
				TextView tv_name = (TextView) v.findViewById(R.id.stock_name);
				System.out.println("itme "+v.getId()+" stock name "+tv_name.getText());
				
				Intent intent = new Intent(context, StockDetailActivity.class);
				intent.putExtra("code", tv_name.getText());
				context.startActivity(intent);
			}
		});
		
		return scrollView;
	}
	
	@Override
	public void onStart() {
		super.onStart();
		
		
		//处理ScrollView滚动时，导航栏的显示与隐藏
		/*scrollView.setOnTouchListener(new OnTouchListener() {
			
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				
				if(event.getAction() == MotionEvent.ACTION_MOVE){
					
					if(event.getY()-scrollView.downY>10){
						//下滑
						parentActivity.tabWidgetIsVisable(true);
					}else {
						//上滑
						if(scrollView.getScrollY() - scrollView.scrollDownY > 10){
							parentActivity.tabWidgetIsVisable(false);
						}
					}
					
					System.out.println("scroll oy"+scrollView.scrollDownY+" ny="+scrollView.getScrollY());
				}
				v.performClick();
				return false;
			}
		});
		*/
		
		zfbTitle.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				
				if(zfbIsClose){
					Drawable drawable= getResources().getDrawable(R.drawable.label_expand);
					/// 这一步必须要做,否则不会显示.
					drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
					tv_zfb.setCompoundDrawables(drawable,null,null,null);
					nslv_zfb.setVisibility(View.VISIBLE);
					zfbIsClose = false;
				}else {
					Drawable drawable= getResources().getDrawable(R.drawable.label_close);
					/// 这一步必须要做,否则不会显示.
					drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
					tv_zfb.setCompoundDrawables(drawable,null,null,null);
					nslv_zfb.setVisibility(View.GONE);
					zfbIsClose = true;
				}
			}
		});
		
		TextView zfbMore = (TextView) zfbTitle.findViewById(R.id.hs_more);
		zfbMore.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(context,HSAStockActivity.class);
				startActivity(intent);
			}
		});
		
	}


	@Override
	public void refresh(Object... params) {
		
		System.out.println("come in");
		List<Stock> stockList = (List<Stock>) params[0];
		
		if(stockList != null){
			for(Stock s : stockList){
				System.out.println("=========>>股票名称："+s.getName()+" 股票代码："+s.getCode()+ " 现价："+s.getNowPrice());
			}
		}
		
		listViewAdapter.setDate(stockList);
		listViewAdapter.notifyDataSetChanged();
		//listViewAdapter.notifyDataSetInvalidated();
		
		
	}
	
}
